﻿using ICSharpCode.AvalonEdit;
using ICSharpCode.AvalonEdit.Document;
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;

namespace VideoTools.Extension
{

    public static class Extensions
    {
        
        public static List<Key> SortedKeys<Key, Value>(this Dictionary<Key, Value> dictionary)
        {
            if (dictionary == null)
            {
                return null;
            }

            List<Key> list = dictionary.Keys.ToList();
            list.Sort();
            return list;
        }
        public static void GotoLineAndHightLight(this TextEditor te,int lineNumber)
        {
            te.ScrollTo(lineNumber, 0);  // 第二个参数是列号（从0开始）
            DocumentLine line = te.Document.GetLineByNumber(lineNumber);
            te.Select(line.Offset, line.Length);
            // 设置默认选中颜色（影响所有选中文本）
            te.TextArea.SelectionBrush = System.Windows.Media.Brushes.Red;       // 背景色
            te.TextArea.SelectionForeground = System.Windows.Media.Brushes.Beige;      // 前景色
            te.TextArea.SelectionBorder = new System.Windows.Media.Pen(System.Windows.Media.Brushes.SpringGreen, 1); // 边框
            te.Focus();
        }

        public static List<string> GetLines(this TextEditor te)
        {
            IList<DocumentLine> lines = te.Document.Lines;
            List<string> txts = new List<string>();
            foreach (DocumentLine line in lines)
            {
                string lineText = te.Document.GetText(line);
                txts.Add(te.Document.GetText(line));
            }
            return txts;
        }

        public static string Desc(this Enum val)
        {
            var type = val.GetType();
            var memberInfo = type.GetMember(val.ToString());
            var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes == null || attributes.Length != 1)
            {
                //如果没有定义描述，就把当前枚举值的对应名称返回
                return val.ToString();
            }
            return (attributes.Single() as DescriptionAttribute).Description;
        }

        public static T GetEnumByDescription<T>(string description) where T : Enum
        {
            System.Reflection.FieldInfo[] fields = typeof(T).GetFields();
            foreach (System.Reflection.FieldInfo field in fields)
            {
                object[] objs = field.GetCustomAttributes(typeof(DescriptionAttribute), false); //获取描述属性
                if (objs.Length > 0 && (objs[0] as DescriptionAttribute).Description == description)
                {
                    return (T)field.GetValue(null);
                }
            }
            return default(T);
        }

        
        public static void ExecuteIfCan(this ICommand Command,object param = null)
        {
            if (Command.CanExecute(null))
                Command.Execute(param);
        }

        public static string Description(this Enum val)
        {
            var type = val.GetType();
            var memberInfo = type.GetMember(val.ToString());
            var attributes = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
            if (attributes == null || attributes.Length != 1)
            {
                //如果没有定义描述，就把当前枚举值的对应名称返回
                return val.ToString();
            }
            return (attributes.Single() as DescriptionAttribute).Description;
        }
        public static void ShowAndFocus(this Window W)
        {
            if (W.IsVisible)
            {
                W.WindowState = WindowState.Normal;
            }

            W.Show();

            W.Activate();
        }

    }
}
